package cn.work.control;

import cn.work.dao.SearchInReservationDao;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "getBeauByServiceIdServlet", value = "/getBeauByServiceIdServlet")
public class getBeauByServiceIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取前端传递的服务ID
        String serviceIdParam = request.getParameter("serviceId");
        if (serviceIdParam == null || serviceIdParam.isEmpty()) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "服务ID不能为空");
            return;
        }

        try {
            int serviceId = Integer.parseInt(serviceIdParam);
            SearchInReservationDao reser = new SearchInReservationDao();
            List<Map<String, Object>> stylists = reser.getStylistsByServiceId(serviceId);

            // 返回JSON数据
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(new Gson().toJson(stylists));
        } catch (Exception e) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "获取美容师失败");
            e.printStackTrace();
        }
    }
}
